#include "mainwindow.h"
#include <QDebug>
#include <QString>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置主窗体的显示位置与大小 */
       this->setGeometry(0, 0, 800, 480);

       setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);//禁用“最大化按钮
       setFixedSize(this->width(), this->height());//禁止调节窗口大小
       /* 实例化对象 */
       comboBox_model = new QComboBox(this);
       label_model = new QLabel("产品型号",this);

       comboBox_version = new QComboBox(this);
       label_version = new QLabel("产品版本",this);

       comboBox_type = new QComboBox(this);
       label_type = new QLabel("产品类型",this);

       comboBox_factory = new QComboBox(this);
       label_factory = new QLabel("生产地",this);

       comboBox_check_year = new QComboBox(this);
       label_check_year = new QLabel("质检年份",this);

       comboBox_check_month = new QComboBox(this);
       label_check_month = new QLabel("质检月",this);

       comboBox_check_day = new QComboBox(this);
       label_check_day = new QLabel("质检日",this);

       comboBox_check_sequence = new QComboBox(this);
       label_check_sequence = new QLabel("质检顺序",this);

       pushBtn_create = new QPushButton("生成SN码",this);
       pushBtn_parse = new QPushButton("解析SN码",this);

       lineEdit = new QLineEdit(this);
       label_sn_out = new QLabel("您生成的SN码是：",this);

       lineEdit_input = new QLineEdit(this);
       label_sn_input = new QLabel("请输入SN码：",this);

       /* 设置comboBox的显示位置与大小 */
       label_model->setGeometry(30, 50, 150, 30);
       comboBox_model->setGeometry(120, 50, 100, 30);

       label_version->setGeometry(30, 100, 150, 30);
       comboBox_version->setGeometry(120,100,100,30);

       label_type->setGeometry(30,150,150,30);
       comboBox_type->setGeometry(120,150,100,30);

       label_factory->setGeometry(30,200,150,30);
       comboBox_factory->setGeometry(120,200,100,30);
       //
       label_check_year->setGeometry(300, 50, 150, 30);
       comboBox_check_year->setGeometry(390, 50, 100, 30);

       label_check_month->setGeometry(300, 100, 150, 30);
       comboBox_check_month->setGeometry(390,100,100,30);

       label_check_day->setGeometry(300,150,150,30);
       comboBox_check_day->setGeometry(390,150,100,30);

       label_check_sequence->setGeometry(300,200,150,30);
       comboBox_check_sequence->setGeometry(390,200,100,30);

       label_sn_out->setGeometry(30, 250, 150, 30);
       lineEdit->setGeometry(30, 280, 150, 30);

       label_sn_input->setGeometry(30,310,150,30);
       lineEdit_input->setGeometry(30,340,150,30);

       pushBtn_create->setGeometry(200, 280, 100, 30);
       pushBtn_parse->setGeometry(200, 340, 100, 30);

       /* 添加项，我们添加三个，作为comboBox的三个选项 */
       comboBox_model->addItem("XZ-SC50");
       comboBox_model->addItem("XZ-WS50");
       comboBox_model->addItem("其他型号");

       for(int i = 0;i<36;i++)
       {
           QString index = QString::number(i+1)  ;
           QString item = "第"+index+"版";
           comboBox_version->addItem(item);
       }

       comboBox_type->addItem("整机");
       comboBox_type->addItem("配件");
       comboBox_type->addItem("其他类型");

       comboBox_factory->addItem("南京C17");
       comboBox_factory->addItem("其他工厂");

       for(int i = 2022;i<2058;i++)
       {
           QString index_year = QString::number(i)  ;
           QString item_year = index_year+"年";
           comboBox_check_year->addItem(item_year);
       }
       for(int i = 0;i<12;i++)
       {
           QString index_month = QString::number(i+1)  ;
           QString item_month = index_month+"月";
           comboBox_check_month->addItem(item_month);
       }
       for(int i = 0;i<31;i++)
       {
           QString index_day = QString::number(i+1)  ;
           QString item_day = index_day+"日";
           comboBox_check_day->addItem(item_day);
       }
       for(int i = 0;i<36;i++)
       {
           QString index_sequence = QString::number(i+1)  ;
           QString item_sequence = "第"+index_sequence+"台";
           comboBox_check_sequence->addItem(item_sequence);
       }
       /* 信号槽连接 */
       connect(comboBox_model, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_modelIndexChanged(int)));
       connect(comboBox_version, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_versionIndexChanged(int)));
       connect(comboBox_type, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_typeIndexChanged(int)));
       connect(comboBox_factory, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_factoryIndexChanged(int)));
       connect(comboBox_check_year, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_check_yearIndexChanged(int)));
       connect(comboBox_check_month, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_check_monthIndexChanged(int)));
       connect(comboBox_check_day, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_check_dayIndexChanged(int)));
       connect(comboBox_check_sequence, SIGNAL(currentIndexChanged(int)), this,
               SLOT(comboBox_check_sequenceIndexChanged(int)));
       connect(pushBtn_create, SIGNAL(clicked()), this, SLOT(pushBtn_create_Clicked()));
       connect(pushBtn_parse, SIGNAL(clicked()), this, SLOT(pushBtn_parse_Clicked()));

}

MainWindow::~MainWindow()
{
}

QString MainWindow::num_to_asc(int value)
{
   QString ret ;
  // value = value+1;
   if(value < 10)
   {
     ret =  QString::number(value);
     return ret;
   }

  if(value == 10)
  {
      ret = 'A';
  }
  else if(value == 11)
  {
      ret = 'B';
  }
  else if(value == 12)
  {
      ret = 'C';
  }
  else if(value == 13)
  {
      ret = 'D';
  }
  else if(value == 14)
  {
      ret = 'E';
  }
  else if(value == 15)
  {
      ret = 'F';
  }
  else if(value == 16)
  {
      ret = 'G';
  }
  else if(value == 17)
  {
      ret = 'H';
  }
  else if(value == 18)
  {
      ret = 'I';
  }
  else if(value == 19)
  {
      ret = 'J';
  }
  else if(value == 20)
  {
      ret = 'K';
  }
  else if(value == 21)
  {
      ret = 'L';
  }
  else if(value == 22)
  {
      ret = 'M';
  }
  else if(value == 23)
  {
      ret = 'N';
  }
  else if(value == 24)
  {
      ret = 'O';
  }
  else if(value == 25)
  {
      ret = 'P';
  }
  else if(value == 26)
  {
      ret = 'Q';
  }
  else if(value == 27)
  {
      ret = 'R';
  }
  else if(value == 28)
  {
      ret = 'S';
  }
  else if(value == 29)
  {
      ret = 'T';
  }
  else if(value == 30)
  {
      ret = 'U';
  }
  else if(value == 31)
  {
      ret = 'V';
  }
  else if(value == 32)
  {
      ret = 'W';
  }
  else if(value == 33)
  {
      ret = 'X';
  }
  else if(value == 34)
  {
      ret = 'Y';
  }
  else if(value == 35)
  {
      ret = 'Z';
  }
  return ret;
}


int MainWindow::asc_to_item(QString string)
{
    int ret = 0 ;
   if(string == '0')
   {
     ret = 0;
   }
   else if(string == '1')
   {
     ret = 1;
   }
   else if(string == '2')
   {
     ret = 2;
   }
   else if(string == '3')
   {
     ret = 3;
   }
   else if(string == '4')
   {
     ret = 4;
   }
   else if(string == '5')
   {
     ret = 5;
   }
   else if(string == '6')
   {
     ret = 6;
   }
   else if(string == '7')
   {
     ret = 7;
   }
   else if(string == '8')
   {
     ret = 8;
   }
   else if(string == '9')
   {
     ret = 9;
   }
   else if(string == 'A')
   {
       ret = 10;
   }
   else if(string == 'B')
   {
       ret = 11;
   }
   else if(string == 'C')
   {
       ret = 12;
   }
   else if(string == 'D')
   {
       ret = 13;
   }
   else if(string == 'E')
   {
       ret = 14;
   }
   else if(string == 'F')
   {
       ret = 15;
   }
   else if(string == 'G')
   {
       ret = 16;
   }
   else if(string == 'H')
   {
       ret = 17;
   }
   else if(string == 'I')
   {
       ret = 18;
   }
   else if(string == 'J')
   {
       ret = 19;
   }
   else if(string == 'K')
   {
       ret = 20;
   }
   else if(string == 'L')
   {
       ret = 21;
   }
   else if(string == 'M')
   {
       ret = 22;
   }
   else if(string == 'N')
   {
       ret = 23;
   }
   else if(string == 'O')
   {
       ret = 24;
   }
   else if(string == 'P')
   {
       ret = 25;
   }
   else if(string == 'Q')
   {
       ret = 26;
   }
   else if(string == 'R')
   {
       ret = 27;
   }
   else if(string == 'S')
   {
       ret = 28;
   }
   else if(string == 'T')
   {
       ret = 29;
   }
   else if(string == 'U')
   {
       ret = 30;
   }
   else if(string == 'V')
   {
       ret = 31;
   }
   else if(string == 'W')
   {
       ret = 32;
   }
   else if(string == 'X')
   {
       ret = 33;
   }
   else if(string == 'Y')
   {
       ret = 34;
   }
   else if(string == 'Z')
   {
       ret = 35;
   }
   return ret;
}
void MainWindow::set_parse_data(int index ,int item)
{
    switch(index)
    {
     case 0:
        comboBox_model->setCurrentIndex(item-1);
        break;
     case 1:
        comboBox_version->setCurrentIndex(item);
        break;
     case 2:
        comboBox_type->setCurrentIndex(item);
       break;
     case 3:
        comboBox_factory->setCurrentIndex(item);
       break;
     case 4:
        comboBox_check_year->setCurrentIndex(item);
       break;
     case 5:
        comboBox_check_month->setCurrentIndex(item);
       break;
     case 6:
        comboBox_check_day->setCurrentIndex(item);
       break;
     case 7:
        comboBox_check_sequence->setCurrentIndex(item);
       break;

    }
}
void MainWindow::pushBtn_create_Clicked()
{
     QString str;
     if(comboBox_model->currentText() == "XZ-SC50")
     {
         str = '1';
     }
     else if(comboBox_model->currentText() == "XZ-WS50")
     {
          str = '2';
     }
     else if(comboBox_model->currentText() == "其他型号")
     {
          str = '3';
     }

     str += num_to_asc(comboBox_version->currentIndex());
     if( comboBox_type->currentText() == "整机")
     {
         str += '0';
     }
     else if( comboBox_type->currentText() == "配件")
     {
         str += '1';
     }
     else if( comboBox_type->currentText() == "其他类型")
     {
         str += '2';
     }

     if(comboBox_factory->currentText() == "南京C17")
     {
         str += '0';
     }
     else if(comboBox_factory->currentText() == "其他工厂")
     {
          str += '1';
     }

     str += num_to_asc(comboBox_check_year->currentIndex());
     str += num_to_asc(comboBox_check_month->currentIndex());
     str += num_to_asc(comboBox_check_day->currentIndex());
     str += num_to_asc(comboBox_check_sequence->currentIndex());
     lineEdit->setText(str);
     qDebug()<<"您的SN码为:"<<str;
}

void MainWindow::pushBtn_parse_Clicked()
{
  QString input_sn = lineEdit_input->text();
  QString str;
  int item ;
  for(int i = 0 ; i < input_sn.length();i++)
  {
      str =input_sn[i] ;
      item = asc_to_item(str);
      set_parse_data(i,item);
  }
}


void MainWindow::comboBox_modelIndexChanged(int index)
{
    qDebug()<<"您选择的type是"<< comboBox_model->itemText(index)<<endl;
}
void MainWindow::comboBox_versionIndexChanged(int index)
{
    qDebug()<<"您选择的version是"<< comboBox_version->itemText(index)<<endl;
}
void MainWindow::comboBox_typeIndexChanged(int index)
{
    qDebug()<<"您选择的type是"<< comboBox_type->itemText(index)<<endl;
}
void MainWindow::comboBox_factoryIndexChanged(int index)
{
    qDebug()<<"您选择的factory是"<< comboBox_factory->itemText(index)<<endl;
}
void MainWindow::comboBox_check_yearIndexChanged(int index)
{
    qDebug()<<index;
    qDebug()<<"您选择的check year是"<< comboBox_check_year->itemText(index)<<endl;
}
void MainWindow::comboBox_check_monthIndexChanged(int index)
{
    qDebug()<<"您选择的check month是"<< comboBox_check_month->itemText(index)<<endl;
}
void MainWindow::comboBox_check_dayIndexChanged(int index)
{
    qDebug()<<"您选择的check day是"<< comboBox_check_day->itemText(index)<<endl;
}
void MainWindow::comboBox_check_sequenceIndexChanged(int index)
{
    qDebug()<<"您选择的check sequence是"<< comboBox_check_sequence->itemText(index)<<endl;
}

